package com.sk.bilibili.model.activity;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.Live;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.FocusMapper;
import com.sk.bilibili.mapper.HistoryMapper;
import com.sk.bilibili.mapper.LiveMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.activity.LiveActivity;

public class LiveModel extends BaseModel<LiveActivity> {

    @Autowired
    LiveMapper liveMapper;
    @Autowired
    FocusMapper focusMapper;
    @Autowired
    HistoryMapper historyMapper;

    public ConstraintLayout.LayoutParams getLandscapeParam() {
        ConstraintLayout.LayoutParams params=new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,ConstraintLayout.LayoutParams.MATCH_PARENT);
        return params;
    }

    public ConstraintLayout.LayoutParams getPortraitParam() {
        ConstraintLayout.LayoutParams params=new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,0);
        params.dimensionRatio="16:9";
        params.topToTop=0;
        return params;
    }

    public void checkLive(Live live) {
        Integer roomId = live.getExtra().getRoomId();
        if(roomId==null){
            view.checkLiveSuccess();
        }else {
            RequestUtils.process(liveMapper.getLiveUrl(roomId), url -> {
                live.setUrl(url);
                view.checkLiveSuccess();
            });
        }
    }

    public void switchFocus(Integer id) {
        RequestUtils.process(focusMapper.switchFocus(id),
                focus -> view.switchFocusSuccess(focus));
    }

    public void addHistory(Live live) {
        JsonMap map = new JsonMap();
        map.put("type",6);
        map.put("targetId",live.getId());
        map.put("title",live.getTitle());
        RequestUtils.process(historyMapper.add(map));
    }

}